<template>
<a-card :bordered="false" class="card-area">
  <div>
      <!-- 查询表单区域 -->
     <!-- <fm-generate-form
      :data="jsonData"
      :remote="remoteFuncs"
      :value="editData"
      :remote-option="dynamicData"
      ref="generateForm"
    >
    </fm-generate-form> -->
    <!-- 查询表单区域 -->
   <el-button type="primary" style="margin-right: 8px;float:right;" @click="reset">重置</el-button>
    <el-button type="primary"  style="margin-right: 8px;float:right;" @click="search">查询</el-button>
  </div>
   <div>
      <div class="operator">
        <a-button type="primary" ghost @click="add">新增</a-button>
        <a-button  @click="batchDelete">删除</a-button>
        <a-dropdown >
          <a-menu slot="overlay">
            <a-menu-item key="export-data" @click="exportExcel">导出Excel</a-menu-item>
          </a-menu>
          <a-button>
            更多操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>
       <!-- 表格区域 替换表格key -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.id"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 900 }"
               @change="handleTableChange">
            <template slot="operation" slot-scope="text, record">
          <a-icon  type="setting" theme="twoTone" twoToneColor="#4a9ff5" @click="edit(record)" title="修改"></a-icon>
            &nbsp;
          <a-icon  type="eye" theme="twoTone" twoToneColor="#42b983" @click="view(record)" title="查看"></a-icon>
        </template>
      </a-table>
    </div>
    <!-- 详细信息查看 -->
    <info
      :InfoData="Info.data"
      :InfoVisiable="Info.visiable"
      @close="handleInfoClose">
    </info>
    <!-- 新增 -->
    <add
      @close="handleAddClose"
      @success="handleAddSuccess"
      :AddData="Add.data"
      :AddVisiable="Add.visiable">
    </add>
    <!-- 修改 -->
    <edit
      ref="Edit"
      @close="handleEditClose"
      @success="handleEditSuccess"
      :EditData="Edit.data"
      :EditVisiable="Edit.visiable">
    </edit>
    </a-card>
</template>

<script>
import Add from './Add'
import Edit from './Edit'
import Info from './Info'
export default {
  name: 'Demo',
  components: {Add, Edit, Info},
  data () {
    return {
      // 查询表单数据区域
      // jsonData: {'list': [{'type': 'grid', 'icon': 'icon-RectangleCopy', 'columns': [{'span': 12, 'list': [{'type': 'grid', 'icon': 'icon-RectangleCopy', 'columns': [{'span': 12, 'list': [{'type': 'input', 'icon': 'icon-input', 'options': {'width': '100%', 'defaultValue': '', 'required': false, 'dataType': 'string', 'pattern': '', 'placeholder': '', 'customClass': '', 'disabled': false, 'labelWidth': 160, 'isLabelWidth': true, 'hidden': false, 'dataBind': true, 'showPassword': false, 'remoteFunc': 'func_1587532310284', 'remoteOption': 'option_1587532310284'}, 'name': '集团商户代码', 'novalid': {}, 'key': '1587532310284', 'model': 'id', 'rules': [{'type': 'string', 'message': '格式不正确'}]}]}, {'span': 12, 'list': [{'type': 'input', 'icon': 'icon-input', 'options': {'width': '100%', 'defaultValue': '', 'required': false, 'dataType': 'string', 'pattern': '', 'placeholder': '', 'customClass': '', 'disabled': false, 'labelWidth': 160, 'isLabelWidth': true, 'hidden': false, 'dataBind': true, 'showPassword': false, 'remoteFunc': 'func_1587532311636', 'remoteOption': 'option_1587532311636'}, 'name': '集团商户名称', 'novalid': {}, 'key': '1587532311636', 'model': 'name', 'rules': [{'type': 'string', 'message': '格式不正确'}]}]}], 'options': {'gutter': 0, 'justify': 'start', 'align': 'top', 'customClass': '', 'hidden': false, 'remoteFunc': 'func_1587532305836', 'remoteOption': 'option_1587532305836'}, 'name': '栅格布局', 'novalid': {}, 'key': '1587532305836', 'model': 'grid_1587532305836', 'rules': []}]}, {'span': 12, 'list': [{'type': 'grid', 'icon': 'icon-RectangleCopy', 'columns': [{'span': 12, 'list': [{'type': 'select', 'icon': 'icon-select', 'options': {'defaultValue': '', 'multiple': false, 'disabled': false, 'clearable': false, 'placeholder': '请选择', 'required': false, 'showLabel': false, 'width': '', 'options': [], 'remote': true, 'remoteType': 'option', 'remoteOption': 'code_option', 'filterable': false, 'remoteOptions': [], 'props': {'value': 'value', 'label': 'label'}, 'remoteFunc': 'func_1587542278633', 'customClass': '', 'labelWidth': 150, 'isLabelWidth': true, 'hidden': false, 'dataBind': true}, 'name': '管理机构代码', 'novalid': {}, 'key': '1587542278633', 'model': 'institution_code', 'rules': []}]}, {'span': 12, 'list': [{'type': 'select', 'icon': 'icon-select', 'options': {'defaultValue': '', 'multiple': false, 'disabled': false, 'clearable': false, 'placeholder': '联盟管理员显示', 'required': false, 'showLabel': false, 'width': '', 'options': [{'value': 'Option 1'}, {'value': 'Option 2'}, {'value': 'Option 3'}], 'remote': true, 'remoteType': 'func', 'remoteOption': 'bank_option', 'filterable': false, 'remoteOptions': [], 'props': {'value': 'value', 'label': 'label'}, 'remoteFunc': 'func_1587542280681', 'customClass': '', 'labelWidth': 100, 'isLabelWidth': false, 'hidden': false, 'dataBind': true}, 'name': '法人行', 'novalid': {}, 'key': '1587542280681', 'model': 'legal_person_bank', 'rules': []}]}], 'options': {'gutter': 0, 'justify': 'start', 'align': 'top', 'customClass': '', 'hidden': false, 'remoteFunc': 'func_1587532307995', 'remoteOption': 'option_1587532307995'}, 'name': '栅格布局', 'novalid': {}, 'key': '1587532307995', 'model': 'grid_1587532307995', 'rules': []}]}], 'options': {'gutter': 0, 'justify': 'start', 'align': 'top', 'customClass': '', 'hidden': false, 'remoteFunc': 'func_1587532304290', 'remoteOption': 'option_1587532304290'}, 'name': '栅格布局', 'key': '1587532304290', 'model': 'grid_1587532304290', 'rules': []}], 'config': {'labelWidth': 100, 'labelPosition': 'right', 'size': 'small', 'customClass': '', 'ui': 'antd', 'layout': 'horizontal', 'labelCol': 13}},
      // editData: {},
      // remoteFuncs: {
      //   func_1587542280681 (resolve) {
      //     // 法人行 legal_person_bank
      //     // resolve(data)
      //   }

      // },
      // dynamicData: {

      //   code_option: [] // 管理机构代码 option data

      // },
      // 查询表单数据区域
      advanced: false,
      Info: {
        visiable: false,
        data: {}
      },
      Add: {
        data: {},
        visiable: false
      },
      Edit: {
        data: {},
        visiable: false
      },
      dataSource: [],
      selectedRowKeys: [],
      queryParams: {},
      sortedInfo: null,
      pagination: {
        pageSizeOptions: ['5', '10', '20', '30', '40', '100'],
        defaultCurrent: 1,
        defaultPageSize: 10,
        showQuickJumper: true,
        showSizeChanger: true,
        showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
      },
      loading: false
    }
  },
  created () {
    // 从此处给下拉选择框或其它组合控件的动态变量赋值
    let params = {id: '111', name: '2312'}
    this.$get('merchant/bank', {...params
    }).then((r) => {
      this.dynamicData.code_option = r.data.data
    }).catch(() => {
    })
  },
  mounted () {
    this.fetch()
  },
  computed: {
    columns () {
      let { sortedInfo, filteredInfo } = this
      sortedInfo = sortedInfo || {}
      filteredInfo = filteredInfo || {}
      return [{
        title: '集团商户代码',
        dataIndex: 'id',
        sorter: true,
        sortOrder: sortedInfo.columnKey === 'id' && sortedInfo.order
      }, {
        title: '集团商户名称',
        dataIndex: 'name',
        sorter: true,
        sortOrder: sortedInfo.columnKey === 'name' && sortedInfo.order
      }, {
        title: '是否集中收款',
        dataIndex: 'isfocuscoll',
        customRender: (text, row, index) => {
          switch (text) {
            case '0':
              return '否'
            case '1':
              return '是'
            default:
              return text
          }
        },
        filters: [
          { text: '否', value: '0' },
          { text: '是', value: '1' }
        ],
        filterMultiple: false,
        filteredValue: filteredInfo.isfocuscoll || null,
        onFilter: (value, record) => record.isfocuscoll.includes(value)
      }, {
        title: '法人行',
        dataIndex: 'bank',
        sorter: true,
        sortOrder: sortedInfo.columnKey === 'bank' && sortedInfo.order
      }, {
        title: '状态',
        dataIndex: 'status',
        customRender: (text, row, index) => {
          switch (text) {
            case '1':
              return <a-tag color="red">修改-审批通过</a-tag>
            case '0':
              return <a-tag color="cyan">新增-审批通过</a-tag>
            default:
              return text
          }
        },
        filters: [
          { text: '修改-审批通过', value: '1' },
          { text: '新增-审批通过', value: '0' }
        ],
        filterMultiple: false,
        filteredValue: filteredInfo.status || null,
        onFilter: (value, record) => record.status.includes(value)
      }, {
        title: '操作',
        dataIndex: 'operation',
        scopedSlots: { customRender: 'operation' },
        fixed: 'right',
        width: 120
      }]
    }
  },
  methods: {
    reset () {
      // 重置查询参数
      this.$refs.generateForm.reset()
      this.fetch()
    },
    search () {
      this.$refs.generateForm.getData().then(data => {
        // Data verification succeeded 数据验证成功
        Object.assign(this.queryParams, data)
        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...this.queryParams,
          ...filteredInfo
        })
      }).catch(e => {
        // Data verification failed 数据验证失败
      })
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    view (record) {
      this.Info.data = record
      this.Info.visiable = true
    },
    add () {
      // 初始化数据
      let intdata = {aa: '123'}
      this.Add.data = intdata
      this.Add.visiable = true
    },
    handleAddClose () {
      this.Add.visiable = false
    },
    handleAddSuccess () {
      this.Add.visiable = false
      this.search()
    },
    edit (record) {
      this.Edit.data = record
      this.Edit.visiable = true
    },
    handleEditClose () {
      this.Edit.visiable = false
    },
    handleEditSuccess () {
      this.Edit.visiable = false
      this.$message.success('修改用户成功')
      this.search()
    },
    handleInfoClose () {
      this.Info.visiable = false
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的记录')
        return
      }
      let that = this
      this.$confirm({
        title: '确定删除所选中的记录?',
        content: '当您点击确定按钮后，这些记录将会被彻底删除，如果其包含子记录，也将一并删除！',
        centered: true,
        onOk () {
          that.$delete('merchant/' + that.selectedRowKeys.join(',')).then(() => {
            that.$message.success('删除成功')
            that.selectedRowKeys = []
            that.fetch()
          })
        },
        onCancel () {
          that.selectedRowKeys = []
        }
      })
    },
    exportExcel () {
      let {sortedInfo} = this
      let sortField, sortOrder
      // 获取当前列的排序和列的过滤规则
      if (sortedInfo) {
        sortField = sortedInfo.field
        sortOrder = sortedInfo.order
      }
      this.$export('merchant/excel', {
        sortField: sortField,
        sortOrder: sortOrder,
        ...this.queryParams
      })
    },
    handleTableChange (pagination, filters, sorter) {
      this.sortedInfo = sorter
      this.fetch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...this.queryParams,
        ...filters
      })
    },
    // 刷新
    fetch (params = {}) {
      this.loading = true
      if (this.paginationInfo) {
        // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
        this.$refs.TableInfo.pagination.current = this.paginationInfo.current
        this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
        params.pageSize = this.paginationInfo.pageSize
        params.pageNum = this.paginationInfo.current
      } else {
        // 如果分页信息为空，则设置为默认值
        params.pageSize = this.pagination.defaultPageSize
        params.pageNum = this.pagination.defaultCurrent
      }
      // 后端数据查询
      this.$get('merchant', {
        ...params
      }).then((r) => {
        let data = r.data
        this.loading = false
        const pagination = { ...this.pagination }
        pagination.total = data.total
        this.dataSource = data.rows
        this.pagination = pagination
      })
    }

  }
}
</script>

<style lang="less" scoped>
@import "../../../static/less/Common";
</style>
